import { ApiBase, Route, src, resHtml, valid } from 'koacat'
import { App, Ctx } from '../../app'

export class UserApi extends ApiBase<App, Ctx> {
  constructor(app: App) {
    super(app)
    app.vite.include('login', src('/services/user/login.vue'))
  }

  public routes: Route<Ctx>[] = [{
    tags: [ 'view' ],
    path: '/',
    middlewares: [],
    get: (ctx: Ctx) => this.loginPage(ctx)
  }]

  public async loginPage(ctx: Ctx) {
    const req = valid<{
      admin?: boolean
    }>({
      type: 'object',
      properties: {
        admin: { type: 'boolean', nullable: true }
      }
    }, ctx.query)
    
    resHtml(ctx, await this.app.vite.render('login', { admin: req.admin }, '登录'))
  }
}
